package simple;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;


public class Consumer {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("simplePushConsumer");
        consumer.setNamesrvAddr("192.168.1.153:9876");
        consumer.subscribe("simple","*");

        //消费者最大线程数
        consumer.setConsumeThreadMax(30);

        //批处理
        consumer.setConsumeMessageBatchMaxSize(10);

        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    String msgId = messageExt.getMsgId();
                    byte[] body = messageExt.getBody();
                    String bodyStr = new String(body);
                    System.out.println("消息msgId{"+msgId+"}: "+bodyStr);
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }
}
